package com.acme.javaschool.intense.sems.sem2concurrency.tracking;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class Tracker {
	
	private final ConcurrentMap<String, Point> map;
	private final Map<String, Point> unmodMap;
	
	public Tracker(ConcurrentMap<String, Point> map) {
		this.map = deepCopy(map);
		unmodMap = Collections.unmodifiableMap(this.map);
	}

	private ConcurrentMap<String, Point> deepCopy(Map<String, Point> mapToCopy) {
		ConcurrentMap<String, Point> newMap = new ConcurrentHashMap<>();

		for (Entry<String, Point> entry : mapToCopy.entrySet()) {
			newMap.put(new String(entry.getKey()), new Point(entry.getValue()));
		}
		
		return newMap; 
	}

	public Point getLocation(String key) {
		return map.get(key);			
	}
	
	public Map<String, Point> getLocations() {
		return unmodMap;
	}
	
	public void setLocation(String key, Point point) {
		//TODO: fix this, make comply to Monitor and Delegated thr.safety
		synchronized (map) {
			map.put(key, new Point(point));
		}
	}
	
}




